package com.yanfan.dlt698_45.frame;

import com.yanfan.dlt698_45.code.AddressType;
import com.yanfan.dlt698_45.code.FrameType;
import com.yanfan.dlt698_45.exception.DLT698_45TransportException;
import io.netty.buffer.ByteBuf;

import java.util.Arrays;

/**
 * @author oliver foo
 * @description
 * @date 2023/3/21 14:30
 */
public class SecurityGetNormalRequest extends SecurityRequest {

    // 通信访问地址，也就是读取数据的属性
    public byte[] objectAddressDesc;
    // mac地址
    private byte[] mac = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
    //序号及优先标志
    private int priorityId = 1;


    public SecurityGetNormalRequest(byte[] address, byte[] objectAddressDesc, byte addressType) throws DLT698_45TransportException {
        super(address, addressType);
        this.objectAddressDesc = Arrays.copyOf(objectAddressDesc, 4);
    }

    public SecurityGetNormalRequest(byte[] address, byte[] objectAddressDesc) throws DLT698_45TransportException {
        super(address, AddressType.SINGLE_ADD);
        this.objectAddressDesc = Arrays.copyOf(objectAddressDesc, 4);
    }

    @Override
    int getFrameLength() {
        return 22 + getAddress().length + getMac().length;
    }

    @Override
    public int getRequestLength() {
        return 8;
    }


    @Override
    void writeRequest(ByteBuf buf) {
        buf.writeByte(FrameType.GET_REQUEST);
        buf.writeByte(FrameType.GET_REQUEST_NORMAL);
        //序号及优先标志
        buf.writeByte(getPriorityId());
        //通信地址
        buf.writeBytes(getObjectAddressDesc());
        // 没有时间标签
        buf.writeByte(0);
    }

    public int getPriorityId() {
        return priorityId;
    }

    public void setPriorityId(int priorityId) {
        this.priorityId = priorityId;
    }

    public byte[] getObjectAddressDesc() {
        return objectAddressDesc;
    }

    @Override
    public byte[] getMac() {
        return this.mac;
    }

    public void setMac(byte[] mac) {
        this.mac = mac;
    }


}
